<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>评论列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/template-web.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">
        评论列表
      </div>
      <div class="container-fluid common_con">
        <table class="table table-striped table-bordered table-hover mp20">
          <thead>
            <tr>
              <th>作者</th>
              <th>评论</th>
              <th>评论在</th>
              <th>提交于</th>
              <th>状态</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>小周</td>
              <td>这是一条测试评论，欢迎光临</td>
              <td>《世界，你好》</td>
              <td>2017-07-04 12:00:00</td>
              <td>已批准</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-warning btn-xs"
                  >拒绝</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr class="danger">
              <td>小右</td>
              <td>你好啊，交个朋友好吗？</td>
              <td>《世界，你好》</td>
              <td>2017-07-06 14:10:00</td>
              <td>待审核</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-info btn-xs"
                  >批准</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr class="danger">
              <td>老周</td>
              <td>不好</td>
              <td>《世界，你好》</td>
              <td>2017-07-09 22:22:00</td>
              <td>待审核</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-info btn-xs"
                  >批准</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr class="danger">
              <td>中周</td>
              <td>How are you?</td>
              <td>《世界，你好》</td>
              <td>2017-07-09 18:22:00</td>
              <td>待审核</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-info btn-xs"
                  >批准</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>小右</td>
              <td>I am fine thank you and you?</td>
              <td>《世界，你好》</td>
              <td>2017-07-11 22:22:00</td>
              <td>已批准</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-warning btn-xs"
                  >拒绝</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>哈哈</td>
              <td>一针见血</td>
              <td>《世界，你好》</td>
              <td>2017-07-22 09:10:00</td>
              <td>已批准</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-warning btn-xs"
                  >拒绝</a
                >
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>武秀英</td>
              <td>
                外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。
              </td>
              <td>《世界，你好》</td>
              <td>1970-03-15 11:31:50</td>
              <td>已拒绝</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>胡娟</td>
              <td>
                采参什正面准观提干在易东统。走部系取团商机酸科往证和流物实则。入程用指学行利划影现清关织方。
              </td>
              <td>《第四篇示例文章》</td>
              <td>1970-03-23 14:16:57</td>
              <td>已拒绝</td>
              <td class="text-center">
                <a href="javascript:void(0);;" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>邵艳</td>
              <td>
                国新研目心思力品家织通还如周气长多。话它思造约众系压程它过去全。必导则达发前何西最老四关向。
              </td>
              <td>《第一篇示例文章》</td>
              <td>1970-04-19 12:34:27</td>
              <td>已拒绝</td>
              <td class="text-center">
                <a
                  href="javascript:editTr( 'trashed',10 );"
                  class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
            <tr>
              <td>唐军</td>
              <td>
                好联律物联使进很们有严这里月之。实养件矿商除政究定划必火起划六。内百那则变次引持只情车各地织持。
              </td>
              <td>《第四篇示例文章》</td>
              <td>1970-04-24 11:22:29</td>
              <td>已批准</td>
              <td class="text-center">
                <a
                  href="javascript:editTr( 'rejected',11 );"
                  class="btn btn-warning btn-xs"
                  >拒绝</a
                >
                <a
                  href="javascript:editTr( 'trashed',11 );"
                  class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
          </tbody>
        </table>
        <div class="row text-center">
          <!-- <ul class="pagination pagination-sm"> -->
          <!-- <li class="page-item first disabled"><a href="#" class="page-link">首页</a></li>
          <li class="page-item prev disabled"><a href="#" class="page-link">上一页</a></li>
          <li class="page-item active"><a href="#" class="page-link">1</a></li>
          <li class="page-item"><a href="#" class="page-link">2</a></li>
          <li class="page-item"><a href="#" class="page-link">3</a></li>
          <li class="page-item"><a href="#" class="page-link">4</a></li>
          <li class="page-item"><a href="#" class="page-link">5</a></li>
          <li class="page-item"><a href="#" class="page-link">6</a></li>
          <li class="page-item"><a href="#" class="page-link">7</a></li>
          <li class="page-item next"><a href="#" class="page-link">下一页</a></li>
          <li class="page-item last"><a href="#" class="page-link">尾页</a></li> -->
          <!-- </ul> -->
           <ul id="pagination" class="pagination-sm"></ul> 
        </div>
      </div>
    </div>
    <script src="./libs/https.js"></script>
    <script src="./libs/jquery.twbsPagination.js"></script>
  </body>

  <!-- 准备评论列表模板 -->
  <script type="text/html" id="tpl-comments">
    {{ each }}
    {{ if $value.state == '待审核' }}
    <tr class="danger">
      {{ else }}
      <tr>
        {{ /if }}
        <td>{{ $value.author}}</td>
        <td>{{ $value.content}}</td>
        <td>《{{ $value.title}}》</td>
        <td>{{ $value.date}}</td>
        <td>{{ $value.state}}</td>
        <td class="text-center">
          {{ if $value.state == '待审核' }}
        <a href="javascript:void(0)" class="btn btn-info btn-xs" dataid="{{ $value.id }}">批准</a>
        {{ else if $value.state == '已通过' }}
        <a href="javascript:void(0)" class="btn btn-warning btn-xs" dataid="{{ $value.id }}">拒绝</a>
        {{ /if }}
        <a href="javascript:void(0)" class="btn btn-danger btn-xs" dataid="{{ $value.id }}">删除</a>
        </td>
      </tr>
    {{ /each }}
  </script>

  <script>
    $(function () {
      //1.封装函数： 获得评论数据，渲染结构
      //pahe：当前选中页码
      let selPage = 1
      function loadData (page) {
        //2.发起 ajax 请求
        $.ajax({
          type: 'get',
          url: BigNew.comment_list,
          data: {
            page: page,
            perpage: 10
          },
          success: function (backData) {
            console.log(backData)
            if (backData.code == 200) {
              let data = backData.data.data
              //3.结合模板引擎，生成标签结构
              let htmlStr = template('tpl-comments', data)
              $('tbody').html(htmlStr)
                //console.log( page );
              //4.加载分页导航
              // 特点: 分页导航的属性只有第一次设置才能生效
              // 解决: 每次重新设置之前,先摧毁原有的分页导航
              $('#pagination').twbsPagination('destroy')
              $('#pagination').twbsPagination({
                //总页数：由后台返回的数据决定
                totalPages: backData.data.totalPage,

                //可见页数
                visiblePages: 10,

                //当前选中页数
                startPage: page,

                //关闭默认的初始化点击事件，避免递归
                initiateStartPageClick: false,

                //修改按钮的文字
                first: '首页',
                last: '尾页',
                next: '下一页',
                prev: '上一页',

                //10.点击页码后执行的回调函数
                onPageClick: function (e, page) {
                  // page: 当前被选中的页码
                  //console.log(page)
                  selPage = page
                  loadData(page)
                }
              })
            }
          }
        })
      }
      //首次加载
      loadData(1)
       
      //5.给所有按钮设置点击事件
      $( 'tbody' ).on( 'click','a',function( e ) {
        //6.确定按钮的目的
        let url = ''
        let text = $( this ).text()
        if( text == '批准' ) {
          url =  BigNew.comment_pass
        }else if(  text == '拒绝' ){
          url =  BigNew.comment_reject
        }else{
          url = BigNew.comment_delete
        }

        //7.获得当前按钮的 id
        let dataid = $( this ).attr( 'dataid' )

        //8.发起 ajax 请求
        $.ajax({
          type: 'post',
          url: url,
          data: {
            id: dataid
          },
          success: function(backData) {
            if( backData.code == 200 ) {
               // 9. 什么情况对页码没有影响,依旧加载当前页数据
            // 什么情况需要加载上一页数据
            if( $( 'tbody tr' ).length  > 1 || text == '批准' ||  text == '拒绝' || selPage == 1 ) {
              //依旧加载当前页面
              loadData( selPage )
            }else{
              loadData( --selPage )
            }
            }
          }
        })
      } )
    })
  </script>
</html>
